package net.sareweb.acab.actions.travel;


import java.util.Date;
import java.util.List;

import javax.faces.event.ActionEvent;

import net.sareweb.acab.components.session.AcabIdentity;
import net.sareweb.acab.entity.Comment;
import net.sareweb.acab.entity.Dialog;
import net.sareweb.acab.entity.manager.CommentManager;
import net.sareweb.acab.entity.manager.DialogManager;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.log.Log;

@Name("interestingTravelsBean")

@Scope(ScopeType.CONVERSATION)
public class InterestingTravelsBean {
	
	@Logger
	private Log log;
	@In 
    AcabIdentity identity;
	@In (create = true)
	DialogManager dialogManager;
	@In(create=true)
	CommentManager commentManager;
	
	private List <Dialog>interestings = null;
	private String newMessage;
	private Integer selectedDialogId;
	private List<Comment> dialogComments;
	
	
	/*Interesting travels are those for whom user created a dialog */
	/*So interesting travels are obtained through dialogs*/
	public List<Dialog> getInterestingTravelDialogs(){
		log.debug("Obtaining interesting travels through dialogs user {0} started", identity.getUser().getIdUser());
		if(interestings!=null){
			log.debug("Already loaded", "");
			return interestings;
		}
		interestings = dialogManager.findDialogByUser(identity.getUser().getIdUser());
		if(interestings!=null)log.debug("Obtained {0} dialog.", interestings.size()); 
		return interestings;
	}
	
	public void selectDialogActionListener(ActionEvent event){
		log.debug("Selected {0} dialog", selectedDialogId);
		Dialog d = dialogManager.find(selectedDialogId);
		d.setOtherSeenDate(new Date());
		dialogManager.save(d);
		updateSelectedDialogComments();
		
	}
	
	public void otherSendMessageActionListener(ActionEvent event){
		log.debug("writing new message {0} by {1} to dialog {2}", newMessage, identity.getUser().getLogin(),selectedDialogId);
		Dialog d =  dialogManager.find(selectedDialogId);
		Comment c = new Comment();
		c.setCreateDate(new Date());
		c.setDialog(d);
		c.setMsg(newMessage);
		c.setUser(identity.getUser());
		commentManager.save(c);
		d.setLastCommentDate(new Date());
		d.setOtherSeenDate(new Date());
		dialogManager.save(d);
		newMessage="";
		updateSelectedDialogComments();
		
	}
	
	
	
	private void updateSelectedDialogComments(){
		dialogComments = commentManager.getDialogComments(selectedDialogId);
	}


	public String getNewMessage() {
		return newMessage;
	}


	public void setNewMessage(String newMessage) {
		this.newMessage = newMessage;
	}

	public Integer getSelectedDialogId() {
		return selectedDialogId;
	}

	public void setSelectedDialogId(Integer selectedDialogId) {
		this.selectedDialogId = selectedDialogId;
	}

	public List<Comment> getDialogComments() {
		return dialogComments;
	}

	public void setDialogComments(List<Comment> dialogComments) {
		this.dialogComments = dialogComments;
	}
	
	
	
	
	
	
	

	
}
